package com.fuxian.action;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fuxian.model.Teacher;
import com.fuxian.service.TeacherService;
import com.fuxian.util.DateUtile;
import com.fuxian.util.ReturnMapUtil;

@Controller
@Scope("prototype")
public class TeacherAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(TeacherAction.class);

	@Resource
	private TeacherService teacherService;

	/*============================================*/
	private int teacher_id;
	private String name;
	private String edu_bg;
	private String intro;
	private String remark1;
	private String photo_path;
	private int rows;
	private int page;
	private int lesson_id;


	public void setTeacher_id(int teacherId) {
		teacher_id = teacherId;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setEdu_bg(String eduBg) {
		edu_bg = eduBg;
	}

	public void setIntro(String intro) {
		this.intro = intro;
	}

	public void setRemark1(String remark1) {
		this.remark1 = remark1;
	}

	public void setPhoto_path(String photoPath) {
		photo_path = photoPath;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setLesson_id(int lessonId) {
		lesson_id = lessonId;
	}
	/*===================================================*/

	public String addTeacher() {
		returnMap = new HashMap<String, Object>();
		Teacher teacher = new Teacher();
		teacher.setName(name);
		teacher.setEdu_bg(edu_bg);
		teacher.setIntro(intro);
		teacher.setPhoto_path(photo_path);
		teacher.setRemark1(remark1);
		teacher.setCreate_time(DateUtile.getTimestamp());
		teacher.setRemark1((String) session.get("user_name"));
		teacherService.addTeacher(teacher);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap,true, "成功！");
		return SUCCESS;
	}

	public String updateTeacher() {
		returnMap = new HashMap<String, Object>();
		Teacher teacher = new Teacher();
		teacher.setTeacher_id(teacher_id);
		teacher.setName(name);
		teacher.setEdu_bg(edu_bg);
		teacher.setIntro(intro);
		teacher.setPhoto_path(photo_path);
		teacher.setRemark1(remark1);
		teacherService.modifyTeacher(teacher);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap,true, "成功！");
		return SUCCESS;
	}

	public String deleteTeacher() {
		returnMap = new HashMap<String, Object>();
		teacherService.deleteTeacherById(teacher_id);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap,true, "成功！");
		return SUCCESS;
	}
	public String getTeacherList() {
		returnMap = new HashMap<String, Object>();
		page = ((page == 0) ? 1 : page);
		rows = ((rows == 0) ? 1 : rows);
		int totalCount=teacherService.selectCount();
		int max_page = (totalCount + rows - 1) / rows;
		returnMap.put("max_page", max_page);
		returnMap.put("page", page);
		returnMap.put("total", totalCount);
		List<Teacher> teacherList = teacherService.selectAllTeacher(rows * (page - 1), rows);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap,true, "成功！");
		returnMap.put("rows", teacherList);
		return SUCCESS;
	}

	

	public String getTeacherInfo() {
		returnMap = new HashMap<String, Object>();
		Teacher teacher = teacherService.selectTeacherById(teacher_id);
		returnMap.put("teacher_id", teacher.getTeacher_id());
		returnMap.put("name", teacher.getName());
		returnMap.put("edu_bg", teacher.getEdu_bg());
		returnMap.put("intro", teacher.getIntro());
		returnMap.put("remark1", teacher.getRemark1());
		returnMap.put("photo_path", teacher.getPhoto_path());
		returnMap.put("create_time", teacher.getCreate_time());
		returnMap.put("create_user", teacher.getCreate_user());
		returnMap = ReturnMapUtil.setReturnHeader(returnMap,true, "成功！");
		return SUCCESS;
	}

	public String getLessonTeachersByLessonId() {
		returnMap = new HashMap<String, Object>();
		List<Teacher> teacherList = teacherService.selectTeachersByLessonId(lesson_id);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap,true, "成功！");
		returnMap.put("rows", teacherList);
		return SUCCESS;
	}
}
